package com.css.zfzx.sjcj.modules.hddcwyStratigraphySvyPoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyStratigraphySvyPoint.repository.HddcWyStratigraphysvypointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyStratigraphySvyPoint.repository.HddcWyStratigraphysvypointRepository;
import com.css.zfzx.sjcj.modules.hddcwyStratigraphySvyPoint.repository.entity.HddcWyStratigraphysvypointEntity;
import com.css.zfzx.sjcj.modules.hddcwyStratigraphySvyPoint.service.HddcWyStratigraphysvypointService;
import com.css.zfzx.sjcj.modules.hddcwyStratigraphySvyPoint.viewobjects.HddcWyStratigraphysvypointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyStratigraphySvyPoint.viewobjects.HddcWyStratigraphysvypointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-01
 */
@Service
public class HddcWyStratigraphysvypointServiceImpl implements HddcWyStratigraphysvypointService {

	@Autowired
    private HddcWyStratigraphysvypointRepository hddcWyStratigraphysvypointRepository;
    @Autowired
    private HddcWyStratigraphysvypointNativeRepository hddcWyStratigraphysvypointNativeRepository;

    @Override
    public JSONObject queryHddcWyStratigraphysvypoints(HddcWyStratigraphysvypointQueryParams queryParams ,
                                                       int curPage, int pageSize) {
        Page<HddcWyStratigraphysvypointEntity> hddcWyStratigraphysvypointPage =
                this.hddcWyStratigraphysvypointNativeRepository.queryHddcWyStratigraphysvypoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyStratigraphysvypointPage);
        return jsonObject;
    }


    @Override
    public HddcWyStratigraphysvypointEntity getHddcWyStratigraphysvypoint(String id) {
        HddcWyStratigraphysvypointEntity hddcWyStratigraphysvypoint = this.hddcWyStratigraphysvypointRepository.findById(id).orElse(null);
         return hddcWyStratigraphysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyStratigraphysvypointEntity saveHddcWyStratigraphysvypoint(HddcWyStratigraphysvypointEntity hddcWyStratigraphysvypoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyStratigraphysvypoint.setId(uuid);
        hddcWyStratigraphysvypoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyStratigraphysvypoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyStratigraphysvypointRepository.save(hddcWyStratigraphysvypoint);
        return hddcWyStratigraphysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyStratigraphysvypointEntity updateHddcWyStratigraphysvypoint(HddcWyStratigraphysvypointEntity hddcWyStratigraphysvypoint) {
        hddcWyStratigraphysvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyStratigraphysvypoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyStratigraphysvypointRepository.save(hddcWyStratigraphysvypoint);
        return hddcWyStratigraphysvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyStratigraphysvypoints(List<String> ids) {
        List<HddcWyStratigraphysvypointEntity> hddcWyStratigraphysvypointList = this.hddcWyStratigraphysvypointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyStratigraphysvypointList) && hddcWyStratigraphysvypointList.size() > 0) {
            for(HddcWyStratigraphysvypointEntity hddcWyStratigraphysvypoint : hddcWyStratigraphysvypointList) {
                this.hddcWyStratigraphysvypointRepository.delete(hddcWyStratigraphysvypoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyStratigraphysvypoint(HddcWyStratigraphysvypointQueryParams queryParams) {
        BigInteger bigInteger = hddcWyStratigraphysvypointNativeRepository.queryHddcWyStratigraphysvypoint(queryParams);
        return bigInteger;
    }

}
